package com.belly.template;

import cn.hutool.core.util.StrUtil;
import com.belly.model.Meta;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Belly
 * @version 1.1.0
 */
public class TemplateUtils {
    public static List<Meta.FileConfig.FileInfo> removeGroupFilesFromRoot(
            List<Meta.FileConfig.FileInfo> fileInfoList){

        //先获取到所有分组信息
        List<Meta.FileConfig.FileInfo> groupFileInfoList = fileInfoList.stream()
                .filter(fileInfo -> StrUtil.isNotBlank(fileInfo.getGroupKey()))
                .collect(Collectors.toList());

        //获取所有分组内的文件列表
        List<Meta.FileConfig.FileInfo> groupFileInfoCollect = groupFileInfoList.stream()
                .flatMap(fileInfo -> fileInfo.getFiles().stream())
                .collect(Collectors.toList());

        //获取所有分组内文件的输入路径
        Set<String> groupFileInputPathCollect = groupFileInfoCollect.stream()
                .map(Meta.FileConfig.FileInfo::getInputPath)
                .collect(Collectors.toSet());

        //移除未分组内中的已分组的文件
        List<Meta.FileConfig.FileInfo> collect = fileInfoList.stream()
                .filter(fileInfo -> !groupFileInputPathCollect.contains(fileInfo.getInputPath()))
                .collect(Collectors.toList());
        return collect;
    }
}
